/**
  2  * zepto插件：向左滑动删除动效
  3  * 使用方法：$('.itemWipe').touchWipe({itemDelete: '.item-delete'});
  4  * 参数：itemDelete  删除按钮的样式名
  5  */
(function($) {
	//$('.list_info')之外的元素按下的时候触发清掉所有左滑效果
	$(document).bind("touchstart", function (event){
		var $target = $(event.target);
		if($target.parents().hasClass('list_info') ){
		}else{
			var children = $('.list_box').children('.list_info');
			children.each(function(i,v){
				v.style.WebkitTransform = "translateX(" + 0 + "px)";
			});
		}
	});
	$.fn.touchWipe = function(option) {
		var defaults = {
			itemDelete: '.item-delete', //删除元素
		};
		var opts = $.extend({}, defaults, option); //配置选项
		var delWidth = $(opts.itemDelete).width();
		var initX; //触摸位置X
		var initY; //触摸位置Y
		var moveX; //滑动时的位置X
		var moveY; //滑动时的位置Y
		var X = 0; //移动距离X
		var Y = 0; //移动距离Y
		var flagX = 0; //是否是左右滑动 0为初始，1为左右，2为上下，在move中设置，在end中归零
		var objX = 0; //目标对象位置
		var old_obj; //上一个滑动出来删除了的对象

		//滑动过程中的方法：实时显示删除按钮位移
		$(this).on('touchstart', function(event) {
			var obj = this;
			initX = event.targetTouches[0].pageX;
			initY = event.targetTouches[0].pageY;
			objX = (obj.style.WebkitTransform.replace(/translateX\(/g, "").replace(/px\)/g, "")) * 1;
			if(objX == 0) {
				//objX == 0说明删除按钮没出来
				$(this).on('touchmove', function(event) {

					// 判断滑动方向，X轴阻止默认事件，Y轴跳出使用浏览器默认
					if(flagX == 0) {
						setScrollX(event);
						//因为做了全页面点击事件，所以这个单独的点击事件就去掉吧
						var children = $('.list_box').children('.list_info');
						children.each(function(i,v){
							v.style.WebkitTransform = "translateX(" + 0 + "px)";
						});
						return;
					} else if(flagX == 1) {
						event.preventDefault();
					} else {
						return;
					}

					var obj = this;
					moveX = event.targetTouches[0].pageX;
					X = moveX - initX;
					if(X >= 0) {
						obj.style.WebkitTransform = "translateX(" + 0 + "px)";
					} else if(X < 0) {
						var l = Math.abs(X);
						obj.style.WebkitTransform = "translateX(" + -l + "px)";
						if(l > delWidth) {
							l = delWidth;
							obj.style.WebkitTransform = "translateX(" + -l + "px)";
						}
					}
				});
			} else if(objX < 0) {
				//objX < 0说明删除按钮已存在
				$(this).on('touchmove', function(event) {

					// 判断滑动方向，X轴阻止默认事件，Y轴跳出使用浏览器默认
					if(flagX == 0) {
						setScrollX(event);
						return;
					} else if(flagX == 1) {
						event.preventDefault();
					} else {
						return;
					}

					var obj = this;
					moveX = event.targetTouches[0].pageX;
					X = moveX - initX;
					if(X >= 0) {
						var r = -delWidth + Math.abs(X);
						obj.style.WebkitTransform = "translateX(" + r + "px)";
						if(r > 0) {
							r = 0;
							obj.style.WebkitTransform = "translateX(" + r + "px)";
						}
					} else { //向左滑动
						obj.style.WebkitTransform = "translateX(" + -delWidth + "px)";
					}
				});
			}
		})

		//滑动结束时判断，并自动滑动到底或返回
		$(this).on('touchend', function(event) {
			var obj = this;
			old_obj = this;

			objX = (obj.style.WebkitTransform.replace(/translateX\(/g, "").replace(/px\)/g, "")) * 1;
			if(objX > -delWidth / 2) {
				obj.style.transition = "all 0.2s";
				obj.style.WebkitTransform = "translateX(" + 0 + "px)";
				obj.style.transition = "all 0";
				objX = 0;
			} else {
				obj.style.transition = "all 0.2s";
				obj.style.WebkitTransform = "translateX(" + -delWidth + "px)";
				obj.style.transition = "all 0";
				objX = -delWidth;
				
			}
			flagX = 0;
		})

		//设置滑动方向
		function setScrollX(event) {
			moveX = event.targetTouches[0].pageX;
			moveY = event.targetTouches[0].pageY;
			X = moveX - initX;
			Y = moveY - initY;
			if(Math.abs(X) > Math.abs(Y)) {
				flagX = 1;
			} else {
				flagX = 2;
			}
			return flagX;
		}
		//链式返回

		return this;
	};
})(Zepto);